<template>
	<div>
		<el-row style="padding-top: 10px">
			<el-row style="margin-bottom: 10px;">
				<el-col :span="24" align="right">
					<el-button type="primary" size="mini" @click="upProjectHandle()" :disabled="project.ProjectStatus == 1">提交人员</el-button>
				</el-col>
			</el-row>
		    <el-col>
		        <el-table :data="YgUserList" style="width: 100%;"  ref="multipleTable"
		            @selection-change="selectionLineChangeHandle" border :row-style="{ height: '20px' }"
		            :cell-style="{ padding: '5px 0 ' }">
		            <el-table-column header-align="center" type="selection" width="50" align="center"></el-table-column>
		            <el-table-column header-align="center" fixed type="index" label="序号" width="50" align="center"
		                :index="_indexMethod"></el-table-column>
		            <el-table-column header-align="center" fixed prop="UserName" label="姓名" align="center">
		            </el-table-column>
		
		            <el-table-column header-align="center" prop="UserNumber" label="工号" align="center">
		            </el-table-column>
		            <el-table-column header-align="center" prop="Sex" label="性别" align="center">
		                <template slot-scope="scope">
		                    <label v-if="scope.row.Sex == 0">男</label>
		                    <label v-else>女</label>
		                </template>
		            </el-table-column>
		            <el-table-column header-align="center" prop="RoleId" label="岗位" align="center"
		                :show-overflow-tooltip="true">
		                <template slot-scope="scope">
		                    {{ RoleNamesByids(scope.row.RoleId) }}
		                </template>
		            </el-table-column>
		            <el-table-column header-align="center" prop="Card" label="身份证号" align="center"
		                width="180"></el-table-column>
		            <el-table-column header-align="center" prop="Phone" label="联系电话" align="center"
		                width="150"></el-table-column>
		            <el-table-column header-align="center" prop="NowFamilyAddress" label="现居地" align="center"
		                minWidth='330' :show-overflow-tooltip="true"></el-table-column>
		            <el-table-column header-align="center" prop="FamilyAddress" label="户籍" align="center" minWidth='330'
		                :show-overflow-tooltip="true">
					</el-table-column>
		            <el-table-column header-align="center" prop="JobNumber" label="工种" align="center"
		                width="110"></el-table-column>
		            <el-table-column header-align="center" prop="InductionTime" label="入职时间" align="center" width="110">
		                <template slot-scope="scope">
		                    {{ _fmtDateGetStr(scope.row.InductionTime, "") }}
		                </template>
		            </el-table-column>
		            <el-table-column header-align="center" prop="DepartureTime" label="离职日期" align="center" width="110">
		                <template slot-scope="scope">
		                    {{ _fmtDateGetStr(scope.row.DepartureTime, "暂未离职") }}
		                </template>
		            </el-table-column>
		
		            <el-table-column header-align="center" prop="BankCardNumber" label="银行卡号" align="center"
		                minWidth='110' :show-overflow-tooltip="true"></el-table-column>
		            <el-table-column header-align="center" prop="BankName" label="开户行" align="center" width="110"
		                :show-overflow-tooltip="true"></el-table-column>
		            <el-table-column header-align="center" prop="InternshipState" label="状态" align="center" width="110">
		                <template slot-scope="scope">
		                    {{ getEmployState(scope.row.InternshipState) }}
		                </template>
		            </el-table-column>
		        </el-table>
		    </el-col>
		</el-row>
	</div>
</template>

<script>
	import * as publicjs from "/public/Scriput/Public"
	import dayjs from "dayjs"
	export default {
		name:'ProjectManagement',
		data(){
			return{
				YgUserList:[],
				UserIds:'', 
			}
		},
		props:{
			project:{
				type:Object,
				required:true
			}
		},
		mounted() {
			this.getYGUserLis()
			this.AllRoleDB = publicjs.GetAllRole(this.$http);
		},
		methods:{
			_indexMethod(index) {
			    return index + 1
			},
			//获取所有人员(员工)
			getYGUserLis(){
				this.$http.post('UserInfo/GetEmployeeUsers').then(res=>{
					const { StatusCode,Data } = res.data || 0
					if(StatusCode == 200){
						this.YgUserList = Data
						this.getProjectYGUserLis()
					} 
					else this.YgUserList = []
				})
			},
			//获取当前项目已分配人员
			getProjectYGUserLis(){
				let _this = this
				let { UserIds } = this.project || ''
				if(UserIds.length>0) {
					let idlist = UserIds.split(',')
					idlist.forEach(x=>{
						let user = _this.YgUserList.find(u=>u.Id == x) || null
						if(user!=null ){
							_this.$nextTick(()=>{
								_this.$refs.multipleTable.toggleRowSelection(user)
							})						
						}
					})
				}	
			},
			//权限名称
			RoleNamesByids(ids) {
			    if (ids != "") {
			        let rolenames = [];
			        let idsstr = ids;
			        let roleids = idsstr.split(',');
			        let _this = this;
			        roleids.forEach(function (item, index) {
			            //item 就是当日按循环到的对象
			            //index是循环的索引，从0开始
			            let iid = parseInt(item);
			            var role = _this.AllRoleDB.find((x) => x.Id == iid);
			            if (role != null) {
			                rolenames.push(role.RoleName);
			            }
			        })
			        return rolenames.join(',');
			    }
			},
			_fmtDateGetStr(obj, str) {
			    if (obj == null || obj == "") {
			        return str;
			    } else {
			        return this.fmtDate(obj);
			    }
			},
			//转换时间戳为年月日,20210108修复负数时间戳的转换问题
			fmtDate(obj) {
			    //var result = "";
			    //if (obj != null || obj != undefined) {
			    //    var data = parseInt(new RegExp(/\d+/).exec(obj)[0]);
			    //    if (obj.indexOf("-") > 0)
			    //        data = -data;
			    //    var date = new Date(data);
			    //    var y = 1900 + date.getYear();
			    //    var m = "0" + (date.getMonth() + 1);
			    //    var d = "0" + date.getDate();
			    //    if (y > 1920 && y < 2099)
			    //        result = y + "-" + m.substring(m.length - 2, m.length) + "-" + d.substring(d.length - 2, d.length);
			    //}			
			    return obj.split(' ')[0];
			},
			getEmployState(obj) {
			    var tate;
			    if (obj == 1)
			        tate = "试用期";
			    else if (obj == 2)
			        tate = "实习期";
			    else if (obj == 4)
			        tate = "正式期";
			    else if (obj == 8)
			        tate = "离职";
			    else if (obj == 16)
			        tate = "兼职";
			    else
			        tate = "未选择";
			    return tate;
			},
			//表格选中项
			selectionLineChangeHandle(value){
				let ids = value.map(x=>{
					return x.Id
				})
				this.UserIds = ids.join(',')
			},
			//修改项目人员
			upProjectHandle(){
				if(this.UserIds.length == 0 ){
					this.$message.warning('请选择项目人员!')
				}else{
					this.$http.post('/Project1/UpProject',{
						Project:{
							...this.project,
							UserIds:this.UserIds	
						}
					}).then(res=>{
						const { StatusCode } = res.data || 0
						if(StatusCode == 200){
							this.$message.success('编辑项目人员成功!')
							setTimeout(()=>{
								this.$parent.$parent.Personnel_Entry.show = false
								this.$emit('updataList')
							},1000)
						}else{
							this.$message.error('编辑项目人员失败,请重试!')
						} 
					})						
				}
			},
			//项目是否完结
			projectOver(){
				const { EndDate } = this.project || ''
				if(EndDate == undefined || EndDate == '') {
					return false
				}else{
					let chazhi = dayjs(EndDate).diff(Date(),'day')
					return chazhi<0 ? true:false
				}
			},
		}
	}
</script>

<style scoped>
</style>